import { PlusOutlined, FileExcelOutlined } from '@ant-design/icons';
import {
  ModalForm,
  ProForm,
  ProFormDateRangePicker,
  ProFormSelect,
  ProFormText,
  ProFormCheckbox,
} from '@ant-design/pro-components';
import { Button, message, notification } from 'antd';
import DownloadExcel from '@/utils/DownloadExcel.js';
import { useEffect, useState } from 'react';
import { useRequest } from 'umi';
import moment from 'moment';

const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

const ExportExcel = ({ dataSource, columns }: any) => {
  const [columnsPrint, setColumnsPrint] = useState([]);
  const [checkedValueArray, setCheckedValueArray] = useState([]);
  useEffect(() => {
    let columnsPrintTemp: any = [];
    columns.map((item: any) => {
      if (item?.hideInTable !== true) {
        columnsPrintTemp.push(item.title);
      }
    });
    console.log(columns);
    let result: any = Array.from(new Set(columnsPrintTemp));
    console.log(result);
    setColumnsPrint(result);
  }, []);

  return (
    <ModalForm
      title="导出Excel"
      trigger={
        <Button type="primary" style={{ marginRight: 10 }}>
          <FileExcelOutlined />
          导出Excel
        </Button>
      }
      autoFocusFirstInput
      modalProps={{
        onCancel: () => console.log('run'),
        destroyOnClose: true,
      }}
      submitTimeout={2000}
      onFinish={async (values) => {
        await waitTime(1000);
        console.log(dataSource);
        console.log(checkedValueArray);
        DownloadExcel(
          checkedValueArray,
          dataSource,
          'EXCEL' + `${moment().format('YYYYMMDDHHmmss')}`,
          [],
          () => {
            setTimeout(() => {
              notification.success({ message: '导出成功!' });
            }, 300);
          },
        );
        return true;
      }}
    >
      <ProForm.Group>
        <ProFormCheckbox.Group
          name="checkbox-group"
          label="请选择导出列"
          // layout={"vertical"}
          options={columnsPrint}
          fieldProps={{
            onChange: (checkedValue) => {
              let checkedValueArrayTemp: any = [];
              checkedValue.map((item: any) => {
                columns.map((itemsub: any) => {
                  if (item === itemsub.title && itemsub.dataIndex !== 'year') {
                    checkedValueArrayTemp.push(itemsub);
                  }
                });
              });
              setCheckedValueArray(checkedValueArrayTemp);
              console.log(checkedValueArrayTemp);
            },
          }}
        />
      </ProForm.Group>
    </ModalForm>
  );
};

export default ExportExcel;
